package com.kangkangqing.utils;


import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtils {
    //提示不能私有化
    private BeanCopyUtils() {

    }

    public static <O, T> T beanCopy(O source, Class<T> clazz) {
        T target = null;
        try {
            target = clazz.newInstance();
            BeanUtils.copyProperties(source, target);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return target;
    }

    public static <O, T> List<T> beanCopyList(List<O> sourceList, Class<T> clazz) {
        return sourceList.stream()
                .map(o -> beanCopy(o, clazz))
                .collect(Collectors.toList());
    }
}
